# -*- coding:utf-8 -*-
import websocket
# import time
import json
import sys

id = sys.argv[1]
ID = int(id)
# 数据定义

def send(ws,data):
    print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<发送消息",json.dumps(data))
    ws.send(json.dumps(data))

def sendstring(ws,data):
    ws.send(data)

def on_message(ws, message):
    global ID
    cmd = json.loads(message)
    print("收到消息>>>>>>>>>>>>>>>>>>>>>>>>>>>>",cmd)
    c=cmd['c']
    m=cmd['m']
    cmd={"c":"user","m":"loaduser","data":{"uid":ID}}
    send(ws,cmd)

def on_error(ws, error):
    print("on error",error)

def on_close(ws):
    print("### closed ###")

def on_open(ws):
    global ID
    print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>on_open succes")
    cmd={"c":"user","m":"login","data":{"uid":ID}}
    send(ws,cmd)
    pass


if __name__ == "__main__":
    url="ws://127.0.0.1:5678/ws_1"
    print(url)
    ws = websocket.WebSocketApp(url,
                              on_message = on_message,
                              on_error = on_error,
                              on_close = on_close)
    ws.on_open = on_open
    ws.run_forever()
